package com.example.healing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.healing.common.lang.Result;
import com.example.healing.entity.SongLike;
import com.example.healing.mapper.SongLikeMapper;
import com.example.healing.service.SongLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author neymar
 * @date 2021年11月30日 15:34
 */
@Service
public class SongLikeImpl extends ServiceImpl<SongLikeMapper, SongLike> implements SongLikeService {

    @Autowired
    private SongLikeMapper likeMapper;

    /**
     * @author neymar
     * @date 2021/12/1 21:45
     * @param like
     * @return com.example.healing.common.lang.Result
     * @description 增加 赞/踩
     */
    @Override
    public Result create(SongLike like) {
        String message = like.getStatus() == "1" ? "点赞":"点踩";
        // 生成id
        String lid = UUID.randomUUID().toString().replace("-", "");
        like.setLid(lid);
        int res =  likeMapper.insert(like);
        return res == -1 ? Result.fail(message+"失败") : Result.succ(message+"成功");
    }

    /**
     * @author neymar
     * @date 2021/12/1 21:45
     * @param songLike
     * @return com.example.healing.common.lang.Result
     * @description 取消 赞/踩
     */
    @Override
    public Result delete(SongLike songLike) {
        String message = "1".equals(songLike.getStatus()) ? "取消点赞":"取消点踩";
        String lid = songLike.getLid();
        int res = likeMapper.deleteById(lid);
        return res == 0 ? Result.fail(message+"失败") : Result.succ(message+"成功");
    }

    /**
     * @author neymar
     * @date 2021/12/1 21:45
     * @param uid
     * @param songId
     * @return com.example.healing.common.lang.Result
     * @description 获取用户对歌曲的 赞/踩
     */
    @Override
    public Result getLike(String uid, String songId) {
        QueryWrapper<SongLike> likeQuery = new QueryWrapper<SongLike>().eq("uid", uid).eq("songId",songId);
        List<SongLike> lists = likeMapper.selectList(likeQuery);
        int res = lists.size();
        return res > 0 ? Result.succ(true) : Result.succ(false);
    }
}
